/**
 * Created by mhy on 2018/8/7.
 */
import * as API from '../../api'
import * as types from '../mutation-types'
const agent = {
  state: {
    agentList: [],
    agentInfo: {},
    updateAgentResult: {},
  },
  getters: {},
  mutations: {
    [types.GET_AGENT_LIST] (state, data){
      let list = data.list.filter(function(item){
        return item.subtype == 'subagent'
      })
      state.agentList =  state.agentList.concat(list)
      console.log(data.list.length)
    },
    [types.AGENT_INFO] (state,data){
      state.agentInfo = data
    },
    [types.UPDATE_SUB_AGENT] (state,data){
      state.updateManageResult = data
    }
  },
  actions: {
    getAgentList({commit, state, rootState}, params){
      var prom = API.subAgent(params)
      prom.then(function (data) {
        commit(types.GET_AGENT_LIST, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateAgent({commit, state, rootState}, params){
      var prom = API.subAgent(params)
      prom.then(function (data) {
        commit(types.UPDATE_SUB_AGENT, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getAgentInfo({commit, state, rootState}, params){
      var prom = API.subAgent(params)
      prom.then(function (data) {
        commit(types.AGENT_INFO, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },

  }
}
export default agent
